// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#ifndef IMAGE_HEADER_H_
#define IMAGE_HEADER_H_

#include <stdint.h>
#include "flash/flash.h"


#pragma pack(push,1)
/**
 * The base header information.
 */
struct image_header_info {
	uint16_t length;			/**< The total length of the header, including the length bytes.*/
	uint16_t format;			/**< The format of the header data. */
	uint32_t marker;			/**< The marker for a valid header. */
};
#pragma pack(pop)

/**
 * Structure for an image header that provides information about the image.  It is intended only to
 * be used as a component within a complete header implementation.
 */
struct image_header {
	struct image_header_info info;			/**< The header information. */
	uint8_t *data;							/**< The header data read from the image. */
};


int image_header_init (struct image_header *header, struct flash *flash, uint32_t addr,
	uint32_t magic_num, size_t max_len);
void image_header_release (struct image_header *header);

int image_header_load_data (struct image_header *header, struct flash *flash, uint32_t addr);
int image_header_get_length (struct image_header *header);


#define	IMAGE_HEADER_ERROR(code)		ROT_ERROR (ROT_MODULE_IMAGE_HEADER, code)

/**
 * Error codes that can be generated by an image header.
 */
enum {
	IMAGE_HEADER_INVALID_ARGUMENT = IMAGE_HEADER_ERROR (0x00),		/**< Input parameter is null or not valid. */
	IMAGE_HEADER_NO_MEMORY = IMAGE_HEADER_ERROR (0x01),				/**< Memory allocation failed. */
	IMAGE_HEADER_NOT_MINIMUM_SIZE = IMAGE_HEADER_ERROR (0x02),		/**< The header length is not the minimum size. */
	IMAGE_HEADER_BAD_MARKER = IMAGE_HEADER_ERROR (0x03),			/**< The header marker is not valid. */
	IMAGE_HEADER_TOO_LONG = IMAGE_HEADER_ERROR (0x04),				/**< The header length exceeds the maximum. */
};


#endif /* IMAGE_HEADER_H_ */
